package com.jdjt.dams.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.app.AbstractJsonController;
import com.base.common.utils.map.MapUtil;
import com.jdjt.dams.entity.DocumentModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class DocumentController extends AbstractJsonController {
	DocumentModel docsModel = new DocumentModel();
	// DocumentModel docModel=null;
	// 默认

	public void index() {
		// DocumentModel docModel =new DocumentModel();
		// setAttr("docModel", docModel);
		setAttr("years", docsModel.getYearOfDoc());
		setAttr("authors", docsModel.getAuthorOfDoc());
		render("docList.jsp");
	}

	public void docList() {

		DocumentModel doc=getModel(DocumentModel.class,"docModel");		
		int pageNumber = (getParaToInt("pageIndex") != null) ? getParaToInt("pageIndex") + 1 : 1;
		int pageSize = (getParaToInt("limit") != null) ? getParaToInt("limit") : 10;
		Page<DocumentModel> docList = docsModel.paginate(pageNumber, pageSize,doc);
		Map<String, Object> map = MapUtil.toModel("rows", docList);
		// map.put("rows",goodsModelList.getList() );
		map.put("results", docList.getTotalRow());
		map.put("hasError", false);
		renderJson(map);
	}

	public void jsonYears() {

		List<DocumentModel> years = docsModel.getYearOfDoc();
		Map<String, Object> map = MapUtil.toListMap("rows", years);
		map.put("results", docsModel.getYearOfDoc());
		map.put("hasError", false);
	}

	/**
	 * 添加页面
	 */
	public void add() {
		render("docAdd.jsp");
	}

	/**
	 * 添加文书
	 */
	public void save() {

		DocumentModel doc = getModel(DocumentModel.class, "docModel");
		int id = DocumentModel.dao.generateId();
		if (doc.get("id") == null) {
			doc.set("id", id);
		}
		if (!doc.save()) {
			render("docAdd.jsp");
		}
		redirect("/ms/doc");
	}

	/**
	 * 修改页面
	 */
	public void edit() {
		setAttr("docModel", DocumentModel.dao.findById(getParaToInt()));
		render("docEdit.jsp");
	}

	// 修改方法
	public void update() {
		getModel(DocumentModel.class, "docModel").update();
		redirect("/ms/doc");
	}

	/**
	 * 删除商品
	 */
	public void delete() {

		Map<String, Object> map = new HashMap<>();
		boolean bool = DocumentModel.dao.deleteById(getParaToInt());
		if (bool) {
			// setAttr("msg", "删除成功");
			map.put("success", true);
			map.put("status", 200);
			renderJson(map);
		} else {
			// setAttr("msg", "添加失败");
			map.put("success", false);
			map.put("status", 400);
			renderJson(map);
		}

	}
	public void deleteBatch(){
		Map<String, Object> map = new HashMap<>();
		Integer[] ides =getParaValuesToInt("ids");		
		int bool = Db.update("delete from da_doc_profile where id in("+ides+")");		
		if (bool>0) {
			// setAttr("msg", "删除成功");
			map.put("success", true);
			map.put("status", 200);
			renderJson(map);
		} else {
			// setAttr("msg", "添加失败");
			map.put("success", false);
			map.put("status", 400);
			renderJson(map);
		}
	}
	// /**
	// * 通过检索条件查询-文件列表
	// */
	// public void ajaxsearchDocList()
	// {
	// String topic =getPara("name");
	// String ayear=getPara("archive");
	// String authors=getPara("author");
	// Page<DocumentModel> docList= docsModel.searchDocList2(1,
	// 10,topic,ayear,authors);
	//// Page<DocumentModel> paged=
	// docsService.searchDocListDynamic(getParaToInt(0, 1), 15,docModel);
	// Map<String, Object> map=MapUtil.toModel("rows", docList);
	//// map.put("rows",goodsModelList.getList() );
	// map.put("results",docList.getTotalRow());
	// map.put("hasError", false);
	// renderJson(map);
	// }
}
